from check_function import *
import chardet
import os


def get_encoding(file):
    # 二进制方式读取，获取字节数据，检测类型
        with open(file, 'rb') as f:
                data = f.read()
                return chardet.detect(data)['encoding']


if __name__=='__main__':
    infile=input('请输入需要处理的文件夹：')
    outfile=input('生成错误的文件夹：')
    if not os.path.exists(outfile):
        os.mkdir(outfile)
    for f in os.listdir(infile):
        efile='error'+f
        file=os.path.join(infile,f)
        errorfile=os.path.join(outfile,efile)
    
        #合法音素集
        phonemeset=['a','ε','e','i','u','ɔ','o','ə',
                    'y','œ','ф','œ~','ε~','ɔ~','ɑ~',
                    'l','m','n','k','ɲ','p','b','d',
                    't','g','ʃ','v','z','Ʒ','s','f','r',
                    'w','j','ɥ']
        #元音音素集
        yuanyinset=['a','ε','e','i','u','ɔ','o','ə',
                    'y','œ','ф','œ~','ε~','ɔ~','ɑ~']
        #合法字符集
        charset=['a','à','â','e','é','è','ê','ë','i',
                 'î','Ï','o','ô','u','û','ù','y','b',
                 'c','d','f','g','h','j','k','l','m',
                 'n','p','q','r','s','t','v','w','x',
                 'z','ç','A','E','I','O','U','Y','B',
                 'C','D','F','G','H','J','K','L','M',
                 'N','P','Q','R','S','T','V','W','X',
                 'Z','Ç']
        #合法标点集
        pronucset=['.',',',':',';','!','?','…',
                '(',')','[',']',"'",'"','-','«','»']
        
        with open(file,'r',encoding=get_encoding(file)) as f:
            #空格
            ybshaokongge=[]
            ybduokonggec=[]
            ybduokonggen=[]

            #结尾字符
            ybend_char_error=[]
            #多斜杠
            ybduoxiegang=[]
    
            #音节元音数量
            ybwuyuanyin=[]
            ybduoyuanyin=[]
            
            #重音
            ciduozhongyinnum=[]
            cizhongyinbiaoji=[]
            cijvshaozhongyin=[]
            cimozhongyin=[]
    
            #音素合法行
            illegal=[]
    
            #文本音标行对齐
            duiqi=[]
    
            #文本行多空格
            wbduokongge=[]
            #文本行有数字
            wbexistnum=[]
            
            #文本行连续标点
            wblianxvbiaodian=[]
            #文本标点位置
            wbbiaodianpos=[]
    
            #韵律符号格式错误
            yunlvgeshi=[]
    
            #句末标点
            sentendpron=[]
    
            #全角符号
            quanjiao=[]
    
            #标点规则
            jvhao=[]
            douhao=[]
            maohao=[]
            fenhao=[]
            tanhao=[]
            wenhao=[]
            shengluehao=[]
            pozhehao=[]
            yuankuohao=[]
            fangkuohao=[]
            shuminghao=[]
            shengwenpie=[]
            yinhao=[]
            lianzifu=[]
    
            ct=f.readlines()
            for i in range(0,len(ct),2):
                sentlist=ct[i].replace('\n','').split('\t')
                num=sentlist[0]
                sent=sentlist[1]
                pron=ct[i+1].replace('\n','').lstrip()
    
                #少空格
                if dotsplash_space(pron):
                    ybshaokongge.append(ct[i])
                    ybshaokongge.append(ct[i+1])
                    ybshaokongge.append(str(dotsplash_space(pron))+'\n')
    ##            if illegal_phoneme(pron,phonemeset):
    ##                ybshaokongge.append(ct[i])
    ##                ybshaokongge.append(ct[i+1])
    
                #多空格
                if more_space_continue(pron):
                    ybduokongge.append(ct[i])
                    ybduokongge.append(ct[i+1])
                    
                if more_space_num(pron):
                    ybduokonggen.append(ct[i])
                    ybduokonggen.append(ct[i+1])
                    ybduokonggen.append(str(more_space_num(pron))+'\n')
    
                #结尾字符
                if end_char(pron,['/',' ']):
                    ybend_char_error.append(ct[i])
                    ybend_char_error.append(ct[i+1])
    
                #多斜杠
                if continue_pronuc(pron,'/'):
                    ybduoxiegang.append(ct[i])
                    ybduoxiegang.append(ct[i+1])
    
                #音节无元音
                if no_yuanyin(pron,yuanyinset):
                    ybwuyuanyin.append(ct[i])
                    ybwuyuanyin.append(ct[i+1])
                    ybwuyuanyin.append(str(no_yuanyin(pron,yuanyinset))+'\n')
    
                #音节多元音
                if more_yuanyin(pron,yuanyinset):
                    ybduoyuanyin.append(ct[i])
                    ybduoyuanyin.append(ct[i+1])
                    ybduoyuanyin.append(str(more_yuanyin(pron,yuanyinset))+'\n')
    
                #多重音
                if stress_num(pron):
                    ciduozhongyinnum.append(ct[i])
                    ciduozhongyinnum.append(ct[i+1])
                    ciduozhongyinnum.append(str(tress_num(pron))+'\n')
    
                #重音数字标记及位置
                if stress_errornum(pron):
                    cizhongyinbiaoji.append(ct[i])
                    cizhongyinbiaoji.append(ct[i+1])
                    cizhongyinbiaoji.append(str(stress_errornum(pron))+'\n')
    
                #少重音
                if stress_existnum(pron):
                    cijvshaozhongyin.append(ct[i])
                    cijvshaozhongyin.append(ct[i+1])
                    cijvshaozhongyin.append(str(stress_existnum(pron))+'\n')
    
                #重音词末元音
                if stress_wordend(pron,yuanyinset):
                    cimozhongyin.append(ct[i])
                    cimozhongyin.append(ct[i+1])
                    cimozhongyin.append(str(stress_wordend(pron,yuanyinset))+'\n')
    
                #音素合法性
                if illegal_phoneme(pron,phonemeset):
                    illegal.append(ct[i])
                    illegal.append(ct[i+1])
                    illegal.append(str(illegal_phoneme(pron,phonemeset))+'\n')
    
                #对齐
                if pronsent_num(sent,pron):
                    duiqi.append('['+str(len(sent.split()))+']'+ct[i])
                    duiqi.append('['+str(len(pron.split('/')))+']'+ct[i+1])
    
                #文本多空格
                if more_space_continue(sent):
                    wbduokongge.append(ct[i])
                    wbduokongge.append(ct[i+1])
    
                #文本行有数字
                if sent_num(sent):
                    wbexistnum.append(ct[i])
                    wbexistnum.append(ct[i+1])
    
                #文本行连续标点
                if continue_pronuc(sent,r'[-/%]'):
                    wblianxvbiaodian.append(ct[i])
                    wblianxvbiaodian.append(ct[i+1])
    
                #韵律格式
                if pronuc_pos(sent,['/','%'],pronucset,charset):
                    yunlvgeshi.append(ct[i])
                    yunlvgeshi.append(ct[i+1])
                    yunlvgeshi.append(str(pronuc_pos(sent,['/','%'],pronucset,charset))+'\n')
    
                #句末标点
                if sent_end_pronc(sent,['.','?','!','…'],['/','%']):
                    sentendpron.append(ct[i])
                    sentendpron.append(ct[i+1])
    
                #全角
                if find_quanjiao(sent):
                    quanjiao.append(ct[i])
                    quanjiao.append(ct[i+1])
        with open(errorfile,'w',encoding='utf-8') as ef:
            ef.write('*'*20+'音标行格式错误'+'*'*20+'\n')
            ef.write('*'*20+'少空格：\n')
            ef.writelines(ybshaokongge)
            ef.write('*'*20+'多空格：\n')
            ef.writelines(ybduokonggec)
            ef.write('*'*20+'多空格(音素与重音符号间)：\n')
            ef.writelines(ybduokonggen)
            ef.write('*'*20+'句子行末出现非法字符(空格与/)：\n')
            ef.writelines(ybend_char_error)
            ef.write('*'*20+'多斜杠：\n')
            ef.writelines(ybduoxiegang)
            ef.write('*'*20+'音标行音节划分问题'+'*'*20+'\n')
            ef.write('*'*20+'音节无元音：\n')
            ef.writelines(ybwuyuanyin)
            ef.write('*'*20+'音节多元音：\n')
            ef.writelines(ybduoyuanyin)
            ef.write('*'*20+'音标行重音符号错误'+'*'*20+'\n')
            ef.write('*'*20+'多重音：\n')
            ef.writelines(ciduozhongyinnum)
            ef.write('*'*20+'单词重音标记错误：\n')
            ef.writelines(cizhongyinbiaoji)
            ef.write('*'*20+'少重音符号：\n')
            ef.writelines(cijvshaozhongyin)
            ef.write('*'*20+'词重音不在词末元音：\n')
            ef.writelines(cimozhongyin)
            ef.write('*'*20+'音标行音素合法性'+'*'*20+'\n')
            ef.writelines(illegal)
            ef.write("*"*20+'单词数与音标数不一致：'+'*'*20+'\n')
            ef.writelines(duiqi)
            ef.write('*'*20+'韵律标注问题（文本行）'+'*'*20+'\n')
            ef.write('*'*20+'手误问题'+'*'*20+'\n')
            ef.write('*'*20+'多空格：\n')
            ef.writelines(wbduokongge)
            ef.write('*'*20+'除编号外出现数字：\n')
            ef.writelines(wbexistnum)
            ef.write('*'*20+'韵律符号格式错误'+'*'*20+'\n')
            ef.write('*'*20+'韵律标记相连：\n')
            ef.writelines(wblianxvbiaodian)
            ef.write('*'*20+'韵律标记格式及句末%问题：\n')
            ef.writelines(yunlvgeshi)
            ef.write('*'*20+'文本语料问题'+'*'*20+'\n')
            ef.write('*'*20+'句末大标点问题：\n')
            ef.writelines(sentendpron)
            ef.write('*'*20+'全角标点问题：\n')
            ef.writelines(quanjiao)
